package cn.qyd.kaoshi;

import java.util.*;

/**
 * @author 邱运铎
 * HJ25 数据分类处理
 * @date 2024-03-23 21:05
 */
public class DataSort {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int m = in.nextInt();
            String[] i_arr = new String[m];
            for (int i = 0; i < m; i++) {
                i_arr[i] = in.next();
            }
            int n = in.nextInt();
            TreeSet<Integer> tSet = new TreeSet<>();
            for (int i = 0; i < n; i++) {
                tSet.add(in.nextInt());
            }
            n = tSet.size();
            String[] r_arr = new String[n];
            Iterator<Integer> iterator = tSet.iterator();
            for (int i = 0;iterator.hasNext();i++) {
                r_arr[i] = String.valueOf(iterator.next());
            }
            int count = 0;
            ArrayList<ArrayList<String>> all = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                ArrayList<String> emp = new ArrayList<>();
                for (int j = 0; j < m; j++) {
                    if(i_arr[j].contains(r_arr[i])) {
                        if(emp.isEmpty()) {
                            emp.add(r_arr[i]);
                            emp.add("0");
                            count++;
                        }
                        emp.add(j+"");
                        emp.add(i_arr[j]);
                        count++;
                    }
                }
                if(!emp.isEmpty()) {
                    emp.set(1,String.valueOf(((int)emp.size()/2)-1));
                    all.add(emp);
                }
            }
            System.out.println(Arrays.toString(i_arr));
            System.out.println(Arrays.toString(r_arr));
            System.out.println(all);
            if (count > 0) {
                System.out.print((count * 2) + " ");
                for (ArrayList<String> list : all) {
                    for (String str : list) {
                        System.out.print(str + " ");
                    }
                }
            }

        }
    }
}
